// functionPage/pages/minePage/myCheckin/reviewNews.js
import throttle from '../../../../utils/throttle.js';
import Http from '../../../../utils/http.js';
import { getCheckInTime } from '../../../../utils/util.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        newsArr: [],
        pageNum: 0,
        loading: true,
        loadingAll: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getNewsList();
    },

    // 获取消息列表
    getNewsList() {
        if (!this.data.loadingAll) {
            this.setData({
                pageNum: this.data.pageNum + 1,
                loading: true,
                loadingAll: false,
            });
            let data = {
                authId: app.globalData.authId,
                origintype: '04',
                pageNo: this.data.pageNum,
                pageSize: 20,
            };
            http.getRequest('cjMap/getRelatedMsgList', data).then((res) => {
                if (res.status == 0) {
                    res.data.forEach((item) => {
                        item.createtime = app.timeago(item.createtime);
                        // 00点赞、01评论、02回复、03提问、04回答提问、05设为优秀、06置顶、07精华、08审核通过、09审核拒绝/删除、10审核推优
                        // 处理审核消息标题
                        if (item.type === '08') {
                            item.review_title = '您的补充打卡通过了审核';
                        }
                        if (item.type === '09') {
                            item.review_title = '抱歉，您的补充打卡未通过审核';
                        }
                        if (item.type === '10') {
                            item.review_title =
                                '恭喜，您的补充打卡被设置为优秀';
                        }
                        item.checkintime = getCheckInTime(item.checkintime);

                        item.detail =
                            item.country === '中国'
                                ? `${item.city} ${item.checkintime}`
                                : `${item.country} ${item.city} ${item.checkintime}`;

                        this.data.newsArr.push(item);
                    });
                    this.setData({
                        newsArr: this.data.newsArr,
                        loading: false,
                    });

                    if (res.pager.currentPage >= res.pager.totalPages) {
                        this.setData({
                            loadingAll: true,
                        });
                    }
                } else {
                    this.setData({
                        loadingAll: true,
                        loading: false,
                    });
                }
            });
        }
    },

    changeNav: throttle(
        function (e) {
            let { nav, id } = e.currentTarget.dataset;
            console.log(nav);
            wx.navigateTo({
                url: `${nav}`,
            });
            const index = this.data.newsArr.findIndex((item) => {
                return item.id === id;
            });
            this.setData({
                [`newsArr[${index}].readflg`]: '01',
            });
        },
        200,
        {
            trailing: false,
        }
    ),
});
